﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq;
using MvcCms.Data;
using MvcCms.Service.Cache;
using Microsoft.Practices.ServiceLocation;
using System.Xml.Linq;

namespace MvcCms.Service.Helpers
{

    public class CustomSelectItem : SelectListItem
    {
        public bool data { get; set; }
    }

    public static class CustomHtmlHelpers
    {
        public static MvcHtmlString CustomDropDownList(this HtmlHelper html, string name, IEnumerable<CustomSelectItem> selectList, object htmlAttributes)
        {
            var selectDoc = XDocument.Parse((html.DropDownList(name, selectList, htmlAttributes).ToHtmlString()));

            var options = from XElement el in selectDoc.Element("select").Descendants()
                            select el;

            foreach (var item in options)
            {
                var itemValue = item.Attribute("value");
                item.SetAttributeValue("data", selectList.Where(x => x.Value == itemValue.Value).Single().data);
            }

            // rebuild the control, resetting the options with the ones you modified
            selectDoc.Root.ReplaceNodes(options.ToArray());
            return MvcHtmlString.Create(selectDoc.ToString());
        }
    }
    
}